import { homePath } from "@/utils";
import { dsFind } from "../datasource";
import { PROJECT_DOMAIN_CLASS_IDENTIFIER, PROJECT_TASK_CLASS_IDENTIFIER } from "@/services/constants";
import { ComparsionOperator } from "../types";
import { transformDomain } from "../transform";
import { ConnectorDataSource } from "@/services/connector";
import { exportTasks } from "../scheduler";

export enum ProjectType {
    AppDev = 'appdev',
    DigitalHuman = 'digital_human',
}

export interface Project {
    id?: number;
    identifier: string;
    project_type: ProjectType;
    name: string;
    title: string;
    icon?: string;
    comment?: string;
    options?: string;
    description?: string;
}

export const WORK_PROJECT_TYPE_SELECT_OPTIONS = [
    { value: 'appdev', label: '应用开发' },
    { value: 'digital_human', label: '数字人' },
];

export const getProjectTypeLable = (ptype: ProjectType) => {
    if (ptype === ProjectType.AppDev) {
        return '应用开发';
    } else if (ptype === ProjectType.DigitalHuman) {
        return '数字人';
    }
    return '';
}

export const projectUpdateCommonToApplication = async (projectIdentifier: string, applicationIdentifier: string): Promise<any> => {
    let home = await homePath();
    let sqliteFile = `${home}/projects/${projectIdentifier}/${applicationIdentifier}/system.sqlite`;
    let tasksPath = `${home}/projects/${projectIdentifier}/${applicationIdentifier}/tasks`;
    //知识域
    try {
        const { total, data } = await dsFind(0, PROJECT_DOMAIN_CLASS_IDENTIFIER, {
            attributes: [{
                name: 'project_identifier',
                operator: ComparsionOperator.Equal,
                value: projectIdentifier
            }]
        });
        //
        let res = await transformDomain({
            id: 0,
            protocol: 'sqlite',
            url: `${home}/rrg01.sqlite`,
            database_type: 'sqlite',
        }, {
            id: 0,
            protocol: 'sqlite',
            url: sqliteFile,
            database_type: 'sqlite',
        }, data.map(item => {
            return {
                domain_identifier: item.domain_identifier
            }
        }), {
            ignore_errs: true,
            ddl: true
        })
    } catch (err) {
        console.log(err);
    }

    //工作流
    try {
        const { total, data } = await dsFind(0, PROJECT_TASK_CLASS_IDENTIFIER, {
            attributes: [{
                name: 'project_identifier',
                operator: ComparsionOperator.Equal,
                value: projectIdentifier
            }]
        });

        let res = await exportTasks(data.map(item => item.task_identifier), tasksPath, {
            id: 0,
            protocol: 'sqlite',
            url: sqliteFile,
            database_type: 'sqlite',
        });

    } catch (error) {
        console.log(error);
    }
    return;
}
